<template>
  <div id="app">
    <el-button type="primary"  @click="addUser()">添加用户</el-button>
    <el-dialog title="发送消息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="form.messageDto.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="内容" :label-width="formLabelWidth">
          <el-input v-model="form.messageDto.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="等级" :label-width="formLabelWidth">
          <el-input v-model="form.messageDto.grade" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上传附件" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            ref="upload1"
            action
            :auto-upload="false"
            :limit="10"
            multiple
            accept="application/jpg"
            :file-list="fileList"
            list-type="picture"
            :on-change="handlerChange"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveUser()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "app",
  data() {
    return {
      msg: "Welcome to Your Vue.js App",
       dialogTableVisible: false,
        dialogFormVisible: false,
        form: {
         messageDto:{
            title:"",
            content:"",
            grade:""

         }
        },
        formLabelWidth: '120px',
        fileList:[],
        imageUrl:"",
        imUrlList:[]
    };
  },methods:{
    addUser(){
      this.dialogFormVisible = true;
    },saveUser(){
    
       var formData = new FormData();
          //将原来form 的值添加到 formData中
          for (var o in this.form) {
            formData.append(o, this.form[o]);
          }
          this.imUrlList.forEach(function(url) {
            formData.append("images", url);
          });
          this.fileList.forEach(function(file) {
            if (file.raw) {
              formData.append("files", file.raw, file.name);
            }
          });

          //设置图片上传的格式
          let config = {
            "Content-Type": "multipart/form-data"
          };

          console.log("formData----------------->", formData);
          for (var key of formData.keys()) {
            console.log("key:{},value:{}", key, formData.get(key));
          }

          var $this = this;
          this.axios({
            method: "post",
            url: "http://localhost:7011/file/upload",
            data: formData,
            config: config
          }).then(function(res) {
            console.log("编辑或者新增返回:{}", res);
            $this.$message({
              message: res.data.msg,
              type: "success"
            });
            if (res.data.code == 200) {
              $this.dialogFormVisible = false;
            }
          });

      this.dialogFormVisible = false
    },
    handlerChange(file, files) {
      this.fileList = files;
      console.log("----fileList:{}",this.fileList);
    },

  }
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>